package com.iris.live.services.report.sources;

import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.BaseReportDataSource;
import com.iris.live.services.report.data.ReportContext;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2018/1/9.
 */
@Component("SalesDailyReportSource")
public class SalesDailyReportSource extends BaseReportDataSource {
    @Override
    public List getData(ReportContext context, List<ReportConfig> config) throws Exception {
        Map info = context.getParams();
        String username = info.get("fr_username").toString(),
                selYear = info.get("selYear").toString(),
                selMonth = info.get("selMonth").toString();
        if (selMonth.length() == 1) {
            selMonth = "0" + selMonth;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateFormatUtils.getFormatDate(selYear + "-" + selMonth + "-01", DateFormatUtils.TIME_FORMAT_STR_YYYY_MM_DD));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String startDate = selYear + "-" + selMonth + "-01",
                endDate = DateFormatUtils.getFormatDateToString(calendar.getTime(), DateFormatUtils.TIME_FORMAT_STR_YYYY_MM_DD);
        List list = getData(username, startDate, endDate);
        return list;
    }

    private List getData (String username, String startDate, String endDate) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select ");
        sql.append(" 	permit,day(stat_date) as day,      ");
        sql.append(" 	clue_num, ");
        sql.append(" 	inroom_clue_num, ");
        sql.append(" 	test_drive_num, ");
        sql.append(" 	quotation_num, ");
        sql.append(" 	exhibition_order_num, ");
        sql.append(" 	dcc_order_num, ");
        sql.append(" 	wholesale_order_num, ");
        sql.append(" 	outside_group_order_num, ");
        sql.append(" 	key_account_order_num, ");
        sql.append(" 	inside_group_order_num, ");
        sql.append(" 	order_num, ");
        sql.append(" 	last_month_residue_order_num, ");
        sql.append(" 	exhibition_invoice_num, ");
        sql.append(" 	dcc_invoice_num, ");
        sql.append(" 	wholesale_invoice_num, ");
        sql.append(" 	outside_group_invoice_num, ");
        sql.append(" 	key_account_invoice_num, ");
        sql.append(" 	inside_group_invoice_num, ");
        sql.append(" 	invoice_num, ");
        sql.append(" 	stock_num, ");
        sql.append(" 	onway_stock_allocation_num, ");
        sql.append(" 	onway_stock_saleable_num, ");
        sql.append(" 	onhand_stock_allocation_num, ");
        sql.append(" 	onhand_stock_saleable_num, ");
        sql.append(" 	onhand_stock_deliverable_num, ");
        sql.append(" 	presale_allocation_num, ");
        sql.append(" 	presale_fake_num, ");
        sql.append(" 	stock_num_60_90, ");
        sql.append(" 	stock_num_90_120, ");
        sql.append(" 	age_larger_120, ");
        sql.append(" 	age_larger_90, ");
        sql.append(" 	monthly_total_stock_saleable_days, ");
        sql.append(" 	total_stock_saleable_days, ");
        sql.append(" 	saleable_stock_saleable_days, ");
        sql.append(" 	after_market_product_amount, ");
        sql.append(" 	after_market_product_gross_amount, ");
        sql.append(" 	avg_after_market_product_amount, ");
        sql.append(" 	total_financial_vehicle_num, ");
        sql.append(" 	dch_financial_vehicle_num, ");
        sql.append(" 	bank_financial_vehicle_num, ");
        sql.append(" 	factory_financial_vehicle_num, ");
        sql.append(" 	financial_vehicle_rate, ");
        sql.append(" 	dch_financial_vehicle_rate, ");
        sql.append(" 	bank_financial_vehicle_rate, ");
        sql.append(" 	factory_financial_vehicle_rate, ");
        sql.append(" 	financial_service_fee_amount, ");
        sql.append(" 	dch_financial_service_fee_amount, ");
        sql.append(" 	bank_financial_service_fee_amount, ");
        sql.append(" 	factory_financial_service_fee_amount, ");
        sql.append(" 	avg_financial_service_fee_amount, ");
        sql.append(" 	avg_dch_financial_service_fee_amount, ");
        sql.append(" 	avg_bank_financial_service_fee_amount, ");
        sql.append(" 	avg_factory_financial_service_fee_amount, ");
        sql.append(" 	new_insurance_vehicle_num, ");
        sql.append(" 	new_insurance_premium_amount, ");
        sql.append(" 	new_insurance_commission_amount, ");
        sql.append(" 	theft_robbery_insurance_vehicle_num, ");
        sql.append(" 	glass_robbery_insurance_vehicle_num, ");
        sql.append(" 	total_extended_warranty_num, ");
        sql.append(" 	other_extended_warranty_num, ");
        sql.append(" 	factory_extended_warranty_num, ");
        sql.append(" 	dealer_extended_warranty_num, ");
        sql.append(" 	dealer_extended_warranty_gross_amount, ");
        sql.append(" 	extended_warranty_gross_amount, ");
        sql.append(" 	avg_extended_warranty_gross_amount, ");
        sql.append(" 	license_num, ");
        sql.append(" 	vehicle_license_service_amount, ");
        sql.append(" 	avg_vehicle_license_service_amount, ");
        sql.append(" 	gp_vehicle_license ");
        sql.append(" from vw_daily_report_data a,用户 b ");
        sql.append(" where a.permit = b.许可 and b.用户名 = :username ");
        sql.append(" and stat_date >= :startDate ");
        sql.append(" and stat_date <= :endDate ");
        Map params = new HashMap();
        params.put("username", username);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        return getList(sql.toString(), params);
    }
}
